
// <copyright file="ControllableAsServiceBaseAdapter.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The controllable as service base adapter.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.ServiceProcess
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ServiceProcess;

    using SubhadraSolutions.Sharp.Utils.Contracts;

    /// <summary>
    /// The controllable as service base adapter.
    /// </summary>
    public class ControllableAsServiceBaseAdapter : ServiceBase, ICompositeControllable, IInitializable
    {
        #region Fields

        /// <summary>
        /// The controller.
        /// </summary>
        private readonly GenericMetricsEnabledComponentController _controller;

        /// <summary>
        ///     Required designer variable.
        /// </summary>
        private IContainer components;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="ControllableAsServiceBaseAdapter"/> class.
        /// </summary>
        public ControllableAsServiceBaseAdapter()
        {
            this.InitializeComponent();
            this.Controllables = new List<IControllable>();
            this._controller = new GenericMetricsEnabledComponentController(this.start, this.Stop, this.pause, this.resume);
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets or sets the controllables.
        /// </summary>
        public IList<IControllable> Controllables { get; set; }

        /// <summary>
        /// Gets the controller.
        /// </summary>
        public IComponentController Controller
        {
            get
            {
                return this._controller;
            }
        }

        /// <summary>
        /// Gets a value indicating whether is initialized.
        /// </summary>
        public bool IsInitialized { get; private set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        #endregion Public Properties

        #region Public Methods and Operators

        /// <summary>
        /// The initialize.
        /// </summary>
        public void Initialize()
        {
            if (!this.IsInitialized)
            {
                GeneralHelper.InitializeInitializables(this.Controllables);
                this.IsInitialized = true;
            }
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The dispose.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            GeneralHelper.DisposeIDisposables(this.Controllables);
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }

            base.Dispose(disposing);
        }

        /// <summary>
        /// The on continue.
        /// </summary>
        protected override void OnContinue()
        {
            this.resume();
        }

        /// <summary>
        /// The on pause.
        /// </summary>
        protected override void OnPause()
        {
            this.pause();
        }

        /// <summary>
        /// The on start.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        protected override void OnStart(string[] args)
        {
            this.start();
        }

        /// <summary>
        /// The on stop.
        /// </summary>
        protected override void OnStop()
        {
            this.stop();
        }

        /// <summary>
        ///     Required method for Designer support - do not modify
        ///     the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new Container();

            // this.ServiceName = "Service1";
        }

        /// <summary>
        /// The pause.
        /// </summary>
        private void pause()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null && controllable.Controller.SupportsPauseAndResume)
                {
                    controllable.Controller.Pause();
                }
            }
        }

        /// <summary>
        /// The resume.
        /// </summary>
        private void resume()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null && controllable.Controller.SupportsPauseAndResume)
                {
                    controllable.Controller.Resume();
                }
            }
        }

        /// <summary>
        /// The start.
        /// </summary>
        private void start()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null)
                {
                    controllable.Controller.Start();
                }
            }
        }

        /// <summary>
        /// The stop.
        /// </summary>
        private void stop()
        {
            foreach (IControllable controllable in this.Controllables)
            {
                if (controllable.Controller != null)
                {
                    controllable.Controller.Stop();
                }
            }
        }

        #endregion Methods
    }
}
